package com.zg.autoform.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zg.autoform.dto.ImportFieldConfig;
import com.zg.autoform.service.ImportTemplateService;
import com.zg.autoform.web.req.DeleteImportTemplateReq;
import com.zg.autoform.web.req.ListImportTemplateFieldConfigReq;
import com.zg.autoform.web.req.ListImportTemplateReq;
import com.zg.autoform.web.req.SaveImportTemplateReq;
import com.zg.common.core.R;
import com.zg.common.core.dao.autoform.DiyImportTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 导入模板管理控制器
 * @author zg
 */
@RestController
@RequestMapping("/import/template")
@RequiredArgsConstructor
@Api(tags = {"导入模板管理"})
public class ImportTemplateController {

    private final ImportTemplateService templateService;

    @ApiOperation("保存导入模板")
    @PostMapping("/save")
    public R<DiyImportTemplate> saveTemplate(@RequestBody @Validated SaveImportTemplateReq req) {
        return templateService.saveTemplate(req);
    }

    @ApiOperation("删除导入模板")
    @PostMapping("/delete")
    public R<?> deleteTemplate(@RequestBody @Validated DeleteImportTemplateReq req) {
        return templateService.deleteTemplate(req);
    }

    @ApiOperation("查询模板列表")
    @PostMapping("/list")
    public R<IPage<DiyImportTemplate>> listTemplates(@RequestBody ListImportTemplateReq req) {
        return templateService.listTemplates(req);
    }

    @ApiOperation("获取模板字段配置")
    @PostMapping("/fields")
    public R<List<ImportFieldConfig>> getTemplateFields(@RequestBody @Validated ListImportTemplateFieldConfigReq req) {
        List<ImportFieldConfig> fieldConfigs = templateService.getTemplateFieldConfigs(req.getTemplateId());
        return R.success(fieldConfigs);
    }
}
